package mx.ipn.persistencia.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import mx.ipn.persistencia.dao.FactorDAO;
import mx.ipn.to.FactorTO;

public class FactorDAOMySQL implements FactorDAO {
	static{FabricaDeDAOsMySQL.iniciaLog(FactorDAOMySQL.class);}

	private static final String INSERT = "INSERT INTO factor (banderazo,valor,descripcion) VALUES(?,?,?)";
	private static final String DELETE = "DELETE FROM factor WHERE id_factor=?";
	private static final String UPDATE = "UPDATE factor SET banderazo=?,valor=?,descripcion=? WHERE id_factor=?";
	private static final String FINDBYID = "SELECT * FROM factor WHERE id_factor=?";
	private static final String SELECT = "SELECT * FROM factor";

	@Override
	public short deleteFactor(int idFactor) {
		try {
			Connection conexion = FabricaDeDAOsMySQL.crearConexion();

			PreparedStatement ps = conexion.prepareStatement(DELETE);

			ps.setInt(1, idFactor);

			ps.executeUpdate();
			if (ps.getUpdateCount() == 0) {
				ps.close();
				conexion.close();
				return 0;
			} else {
				ps.close();
				conexion.close();
				return 1;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return (short)-1;
		}
	}

	@Override
	public FactorTO findFactorById(int idFactor) {
		try {
			Connection conexion = FabricaDeDAOsMySQL.crearConexion();

			PreparedStatement ps = conexion.prepareStatement(FINDBYID);

			ps.setInt(1, idFactor);

			ResultSet rs = ps.executeQuery();

			if (rs.next()) {
				float banderazo = rs.getFloat("banderazo");
				float valor = rs.getFloat("valor");
				String descripcion = rs.getString("descripcion");
				
				FactorTO factorTO = new FactorTO(idFactor, banderazo, valor, descripcion);
				ps.close();
				conexion.close();
				return factorTO;
			}
			ps.close();
			conexion.close();
			return null;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public boolean insertFactor(FactorTO factorTO) {
		try {
			Connection conexion = FabricaDeDAOsMySQL.crearConexion();

			PreparedStatement ps = conexion.prepareStatement(INSERT);

			ps.setDouble(1, factorTO.getBanderazo());
			ps.setDouble(2, factorTO.getValor());
			ps.setString(3, factorTO.getDescripcion());
			ps.executeUpdate();

			ps.close();
			conexion.close();

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public ArrayList<FactorTO> selectFactor() {
		try {
			Connection conexion = FabricaDeDAOsMySQL.crearConexion();

			PreparedStatement ps = conexion.prepareStatement(SELECT);

			ResultSet rs = ps.executeQuery();

			ArrayList<FactorTO> coleccion = null;

			while (rs.next()) {
				if (coleccion == null)
					coleccion = new ArrayList<FactorTO>();

				int idFactor = rs.getInt("id_factor");
				float banderazo = rs.getFloat("banderazo");
				float valor = rs.getFloat("valor");
				String descripcion = rs.getString("descripcion");

				FactorTO factorTO = new FactorTO(idFactor, banderazo, valor, descripcion);
				coleccion.add(factorTO);
			}
			ps.close();
			conexion.close();

			return coleccion;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public short updateFactor(FactorTO factorTO) {
		try {
			Connection conexion = FabricaDeDAOsMySQL.crearConexion();

			PreparedStatement ps = conexion.prepareStatement(UPDATE);

			ps.setDouble(1, factorTO.getBanderazo());
			ps.setDouble(2, factorTO.getValor());
			ps.setString(3, factorTO.getDescripcion());
			ps.setInt(4, factorTO.getIdFactor());

			ps.executeUpdate();

			if (ps.getUpdateCount() == 0) {
				ps.close();
				conexion.close();
				return 0;
			} else {
				ps.close();
				conexion.close();
				return 1;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return (short)-1;
		}
	}

}
